/**
 * 
 */
package com.cdhospital.Utils;

import java.io.IOException;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;


/**
 * @author weirdor
 *
 */
public class QiniuUtils {
	 //设置好账号的ACCESS_KEY和SECRET_KEY
    String ACCESS_KEY = "J_KpvvtDLwP_8TLLVG90q5AHEVbO-ZjLpJrgA9zK";
    String SECRET_KEY = "X4gXFQ3z-KfHOOEtcVI7E0MEEchijJgwFl9kRN-q";
    //要上传的空间
    String bucketname = "cdhospital";
    //配置域名
    private static final String BUCKET_HOST_NAME = "http://oqif9b9q0.bkt.clouddn.com";  
    //密钥配置  
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);  
    //创建上传对象  
    UploadManager uploadManager = new UploadManager();  

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了  
    public String getUpToken(){  
        return auth.uploadToken(bucketname);  
    }  
    //普通上传  
    public String upload( String key,String FilePath) throws IOException{  
      try {  
        //调用put方法上传  
        Response res = uploadManager.put(FilePath, key, getUpToken());  
        //打印返回的信息  
        System.out.println(res.bodyString());   
        String buck=BUCKET_HOST_NAME+"//"+key;
        return buck;
        } catch (QiniuException e) {  
            Response r = e.response;  
            // 请求失败时打印的异常的信息  
            System.out.println(r.toString());  
        }  
      return null;
    }  

}
